<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />

  <title>Class: RubyEvents::Events</title>

  <link rel="stylesheet" href="../rdoc.css" type="text/css" media="screen" />

  <script src="../js/jquery.js" type="text/javascript" charset="utf-8"></script>
  <script src="../js/thickbox-compressed.js" type="text/javascript" charset="utf-8"></script>
  <script src="../js/quicksearch.js" type="text/javascript" charset="utf-8"></script>
  <script src="../js/darkfish.js" type="text/javascript" charset="utf-8"></script>

</head>
<body id="top" class="class">

  <div id="metadata">
    <div id="home-metadata">
      <div id="home-section" class="section">
        <h3 class="section-header">
          <a href="../index.html">Home</a>
          <a href="../index.html#classes">Classes</a>
          <a href="../index.html#methods">Methods</a>
        </h3>
      </div>
    </div>

    <div id="file-metadata">
      <div id="file-list-section" class="section">
        <h3 class="section-header">In Files</h3>
        <div class="section-body">
          <ul>
          
            <li><a href="../lib/ruby_events/core_rb.html?TB_iframe=true&amp;height=550&amp;width=785"
              class="thickbox" title="lib/ruby_events/core.rb">lib/ruby_events/core.rb</a></li>
          
          </ul>
        </div>
      </div>

      
    </div>

    <div id="class-metadata">
      
      <!-- Parent Class -->
      <div id="parent-class-section" class="section">
        <h3 class="section-header">Parent</h3>
        
        <p class="link"><a href="../Object.html">Object</a></p>
        
      </div>
      

      

      

      
      <!-- Method Quickref -->
      <div id="method-list-section" class="section">
        <h3 class="section-header">Methods</h3>
        <ul class="link-list">
          
          <li><a href="#method-c-new">::new</a></li>
          
          <li><a href="#method-c-version">::version</a></li>
          
          <li><a href="#method-i-fire">#fire</a></li>
          
          <li><a href="#method-i-fire_on_method">#fire_on_method</a></li>
          
          <li><a href="#method-i-listen">#listen</a></li>
          
          <li><a href="#method-i-remove">#remove</a></li>
          
          <li><a href="#method-i-remove_fire_on_method">#remove_fire_on_method</a></li>
          
        </ul>
      </div>
      

      
    </div>

    <div id="project-metadata">
      
      

      <div id="classindex-section" class="section project-section">
        <h3 class="section-header">Class/Module Index
          <span class="search-toggle"><img src="../images/find.png"
            height="16" width="16" alt="[+]"
            title="show/hide quicksearch" /></span></h3>
        <form action="#" method="get" accept-charset="utf-8" class="initially-hidden">
        <fieldset>
          <legend>Quicksearch</legend>
          <input type="text" name="quicksearch" value=""
            class="quicksearch-field" />
        </fieldset>
        </form>

        <ul class="link-list">
        
          <li><a href="../RubyEvents.html">RubyEvents</a></li>
        
          <li><a href="../RubyEvents/Events.html">RubyEvents::Events</a></li>
        
          <li><a href="../Object.html">Object</a></li>
        
        </ul>
        <div id="no-class-search-results" style="display: none;">No matching classes.</div>
      </div>

      
    </div>
  </div>

  <div id="documentation">
    <h1 class="class">RubyEvents::Events</h1>

    <div id="description" class="description">
      
<p>The <a href="Events.html">Events</a> class. Contains all the methods and
functionality that drives the actual processes of adding, listening,
calling and receiving events.</p>

    </div><!-- description -->

    
    <div id="5Buntitled-5D" class="documentation-section">
      

      

      

      

      <!-- Methods -->
      
      <div id="public-class-method-details" class="method-section section">
        <h3 class="section-header">Public Class Methods</h3>

      
        <div id="new-method" class="method-detail ">
          <a name="method-c-new"></a>

          
          <div class="method-heading">
            <span class="method-name">new</span><span
              class="method-args">(parent)</span>
            <span class="method-click-advice">click to toggle source</span>
          </div>
          

          <div class="method-description">
            
            <p>Initialize the events class by instantiating the class methods we’ll be
using.</p>
            

            
            <div class="method-source-code" id="new-source">
<pre>
<span class="ruby-comment"># File lib/ruby_events/core.rb, line 17</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">initialize</span>(<span class="ruby-identifier">parent</span>)
  <span class="ruby-ivar">@parent</span>, <span class="ruby-ivar">@events</span> = <span class="ruby-identifier">parent</span>, {}
<span class="ruby-keyword">end</span></pre>
            </div><!-- new-source -->
            
          </div>

          

          
        </div><!-- new-method -->

      
        <div id="version-method" class="method-detail ">
          <a name="method-c-version"></a>

          
          <div class="method-heading">
            <span class="method-name">version</span><span
              class="method-args">()</span>
            <span class="method-click-advice">click to toggle source</span>
          </div>
          

          <div class="method-description">
            
            <p>The current version of <a href="../RubyEvents.html">RubyEvents</a>.</p>
            

            
            <div class="method-source-code" id="version-source">
<pre>
<span class="ruby-comment"># File lib/ruby_events/core.rb, line 11</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">version</span>
  <span class="ruby-string">'1.1.0'</span>
<span class="ruby-keyword">end</span></pre>
            </div><!-- version-source -->
            
          </div>

          

          
        </div><!-- version-method -->

      
      </div><!-- public-class-method-details -->
    
      <div id="public-instance-method-details" class="method-section section">
        <h3 class="section-header">Public Instance Methods</h3>

      
        <div id="fire-method" class="method-detail ">
          <a name="method-i-fire"></a>

          
          <div class="method-heading">
            <span class="method-name">fire</span><span
              class="method-args">(event_type, *arguments)</span>
            <span class="method-click-advice">click to toggle source</span>
          </div>
          

          <div class="method-description">
            
            <p>Fire all registered listeners to the passed event, passing them the
arguments as provided.</p>
            

            
            <div class="method-source-code" id="fire-source">
<pre>
<span class="ruby-comment"># File lib/ruby_events/core.rb, line 37</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">fire</span>(<span class="ruby-identifier">event_type</span>, *<span class="ruby-identifier">arguments</span>)
  <span class="ruby-ivar">@events</span>[<span class="ruby-identifier">event_type</span>].<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">event</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">event</span>.<span class="ruby-identifier">call</span>(*<span class="ruby-identifier">arguments</span>)
  <span class="ruby-keyword">end</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">event_is_defined</span>(<span class="ruby-identifier">event_type</span>)
<span class="ruby-keyword">end</span></pre>
            </div><!-- fire-source -->
            
          </div>

          

          
        </div><!-- fire-method -->

      
        <div id="fire_on_method-method" class="method-detail ">
          <a name="method-i-fire_on_method"></a>

          
          <div class="method-heading">
            <span class="method-name">fire_on_method</span><span
              class="method-args">(method, event_type, &block)</span>
            <span class="method-click-advice">click to toggle source</span>
          </div>
          

          <div class="method-description">
            
            <p>Set an event to fire when passed method is called. This is useful for
adding callbacks or events to built-in methods.</p>
            

            
            <div class="method-source-code" id="fire_on_method-source">
<pre>
<span class="ruby-comment"># File lib/ruby_events/core.rb, line 45</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">fire_on_method</span>(<span class="ruby-identifier">method</span>, <span class="ruby-identifier">event_type</span>, &amp;<span class="ruby-identifier">block</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">method</span>.<span class="ruby-identifier">is_a?</span> <span class="ruby-constant">Array</span>
    <span class="ruby-identifier">method</span>.<span class="ruby-identifier">map</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">sym</span><span class="ruby-operator">|</span> <span class="ruby-identifier">fire_on_method</span>(<span class="ruby-identifier">sym</span>, <span class="ruby-identifier">event_type</span>, &amp;<span class="ruby-identifier">block</span>) }
    <span class="ruby-keyword">return</span>
  <span class="ruby-keyword">end</span>
  
  <span class="ruby-comment"># We alias @parent to parent here, because class_eval can't see outside</span>
  <span class="ruby-comment"># this scope otherwise.</span>
  <span class="ruby-identifier">parent</span>, <span class="ruby-identifier">old_method</span> = <span class="ruby-ivar">@parent</span>, (<span class="ruby-string">'ruby_events_'</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">method</span>.<span class="ruby-identifier">to_s</span> <span class="ruby-operator">+</span> <span class="ruby-string">'_event_old'</span>).<span class="ruby-identifier">to_sym</span>
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">parent</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">parent</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">method</span>)
    <span class="ruby-identifier">parent</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">class_eval</span> <span class="ruby-keyword">do</span>
      <span class="ruby-comment"># If the parent is already responding to the alias method, it means</span>
      <span class="ruby-comment"># the fire_on_method event was already triggered. Remove the other</span>
      <span class="ruby-comment"># event and continue if this happens.</span>
      <span class="ruby-keyword">if</span> <span class="ruby-identifier">parent</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">old_method</span>, <span class="ruby-keyword">true</span>)
        <span class="ruby-identifier">remove_method</span> <span class="ruby-identifier">method</span>
        <span class="ruby-identifier">alias_method</span> <span class="ruby-identifier">method</span>, <span class="ruby-identifier">old_method</span>
        <span class="ruby-identifier">remove_method</span> <span class="ruby-identifier">old_method</span>
      <span class="ruby-keyword">end</span>
    
      <span class="ruby-identifier">alias_method</span> <span class="ruby-identifier">old_method</span>, <span class="ruby-identifier">method</span>
      <span class="ruby-identifier">private</span> <span class="ruby-identifier">old_method</span>
      
      <span class="ruby-comment"># Make sure the self.send is at the end, or we won't return what we</span>
      <span class="ruby-comment"># are supposed to.</span>
      <span class="ruby-identifier">define_method</span> <span class="ruby-identifier">method</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span>*<span class="ruby-identifier">args</span><span class="ruby-operator">|</span>
        <span class="ruby-identifier">events</span>.<span class="ruby-identifier">fire</span>(<span class="ruby-identifier">event_type</span>, *<span class="ruby-identifier">args</span>)
        <span class="ruby-identifier">block</span>.<span class="ruby-identifier">call</span>(*<span class="ruby-identifier">args</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">block</span> <span class="ruby-comment"># Calling the block we've been passed</span>
        <span class="ruby-identifier">__send__</span>(<span class="ruby-identifier">old_method</span>, *<span class="ruby-identifier">args</span>)
      <span class="ruby-keyword">end</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">raise</span> <span class="ruby-string">'The given object does not respond to method you are trying to intercept calls to.'</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
            </div><!-- fire_on_method-source -->
            
          </div>

          

          
        </div><!-- fire_on_method-method -->

      
        <div id="listen-method" class="method-detail ">
          <a name="method-i-listen"></a>

          
          <div class="method-heading">
            <span class="method-name">listen</span><span
              class="method-args">(event_type, procs = nil, &block)</span>
            <span class="method-click-advice">click to toggle source</span>
          </div>
          

          <div class="method-description">
            
            <p>Add a listener to the passed event type. You can pass a Proc, an array of
Proc’s, or a block.</p>
            

            
            <div class="method-source-code" id="listen-source">
<pre>
<span class="ruby-comment"># File lib/ruby_events/core.rb, line 23</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">listen</span>(<span class="ruby-identifier">event_type</span>, <span class="ruby-identifier">procs</span> = <span class="ruby-keyword">nil</span>, &amp;<span class="ruby-identifier">block</span>)
  <span class="ruby-ivar">@events</span>[<span class="ruby-identifier">event_type</span>] = [] <span class="ruby-keyword">unless</span> <span class="ruby-identifier">event_is_defined</span>(<span class="ruby-identifier">event_type</span>)
  <span class="ruby-identifier">procs_collected</span> = []
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">procs</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value">:each</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">procs</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value">:to_a</span>)
    <span class="ruby-identifier">procs_collected</span> <span class="ruby-operator">+=</span> <span class="ruby-identifier">procs</span>.<span class="ruby-identifier">to_a</span>
  <span class="ruby-keyword">elsif</span> <span class="ruby-identifier">procs</span>
    <span class="ruby-identifier">procs_collected</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">procs</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-identifier">procs_collected</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">block</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">block</span>
  <span class="ruby-ivar">@events</span>[<span class="ruby-identifier">event_type</span>] <span class="ruby-operator">+=</span> <span class="ruby-identifier">procs_collected</span>
<span class="ruby-keyword">end</span></pre>
            </div><!-- listen-source -->
            
          </div>

          

          
        </div><!-- listen-method -->

      
        <div id="remove-method" class="method-detail ">
          <a name="method-i-remove"></a>

          
          <div class="method-heading">
            <span class="method-name">remove</span><span
              class="method-args">(event_type, event)</span>
            <span class="method-click-advice">click to toggle source</span>
          </div>
          

          <div class="method-description">
            
            <p>Remove a method from the listening queue.</p>
            

            
            <div class="method-source-code" id="remove-source">
<pre>
<span class="ruby-comment"># File lib/ruby_events/core.rb, line 82</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">remove</span>(<span class="ruby-identifier">event_type</span>, <span class="ruby-identifier">event</span>)
  <span class="ruby-ivar">@events</span>[<span class="ruby-identifier">event_type</span>].<span class="ruby-identifier">delete_if</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">stored_event</span><span class="ruby-operator">|</span> <span class="ruby-identifier">stored_event</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">event</span>} <span class="ruby-keyword">if</span> <span class="ruby-identifier">event_is_defined</span>(<span class="ruby-identifier">event_type</span>)
<span class="ruby-keyword">end</span></pre>
            </div><!-- remove-source -->
            
          </div>

          

          
        </div><!-- remove-method -->

      
        <div id="remove_fire_on_method-method" class="method-detail ">
          <a name="method-i-remove_fire_on_method"></a>

          
          <div class="method-heading">
            <span class="method-name">remove_fire_on_method</span><span
              class="method-args">(method)</span>
            <span class="method-click-advice">click to toggle source</span>
          </div>
          

          <div class="method-description">
            
            <p>Remove a fire on method alias event.</p>
            

            
            <div class="method-source-code" id="remove_fire_on_method-source">
<pre>
<span class="ruby-comment"># File lib/ruby_events/core.rb, line 87</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">remove_fire_on_method</span>(<span class="ruby-identifier">method</span>)
  <span class="ruby-identifier">parent</span>, <span class="ruby-identifier">old_method</span> = <span class="ruby-ivar">@parent</span>, (<span class="ruby-string">'ruby_events_'</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">method</span>.<span class="ruby-identifier">to_s</span> <span class="ruby-operator">+</span> <span class="ruby-string">'_event_old'</span>).<span class="ruby-identifier">to_sym</span>
  <span class="ruby-identifier">parent</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">class_eval</span> <span class="ruby-keyword">do</span>
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">parent</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">old_method</span>, <span class="ruby-keyword">true</span>)
      <span class="ruby-identifier">remove_method</span> <span class="ruby-identifier">method</span>
      <span class="ruby-identifier">alias_method</span> <span class="ruby-identifier">method</span>, <span class="ruby-identifier">old_method</span>
      <span class="ruby-identifier">public</span> <span class="ruby-identifier">method</span>
    <span class="ruby-keyword">else</span>
      <span class="ruby-identifier">raise</span> <span class="ruby-string">'The fire on method trigger you were trying to remove does not exist.'</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
            </div><!-- remove_fire_on_method-source -->
            
          </div>

          

          
        </div><!-- remove_fire_on_method-method -->

      
      </div><!-- public-instance-method-details -->
    
    </div><!-- 5Buntitled-5D -->
  

  </div><!-- documentation -->

  <div id="validator-badges">
    <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
    <p><small>Generated with the <a href="http://deveiate.org/projects/Darkfish-Rdoc/">Darkfish
      Rdoc Generator</a> 2</small>.</p>
  </div>

</body>
</html>

